package logic

import (
	"context"
	"errors"
	"fresh-box-mart/freshrpc/freshrpc"
	"fresh-box-mart/freshrpc/internal/svc"
	"fresh-box-mart/model/mysql"

	"github.com/zeromicro/go-zero/core/logx"
)

type FreshAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewFreshAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FreshAddLogic {
	return &FreshAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *FreshAddLogic) FreshAdd(in *freshrpc.FreshAddRequest) (*freshrpc.FreshAddResponse, error) {
	// todo: add your logic here and delete this line
	fresh := mysql.Fresh{
		Title: in.Title,
		Image: in.Image,
		Price: float64(in.Price),
		Typer: in.Typer,
	}
	if fresh.Title == in.Title {
		return nil, errors.New("商品已经存在")
	}
	if fresh.Typer == "" {
		fresh.Typer = "正常"
	}
	err := fresh.FreshAdd(mysql.DB)
	if err != nil {
		return nil, err
	}

	return &freshrpc.FreshAddResponse{
		FreshId: int64(fresh.ID),
	}, nil
}
